package com.itheima.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    @Select("select count(1) from t_order where ORDERDATE =#{orderDate} and SETMEAL_ID = #{setmealId} and MEMBER_ID =#{id}")
    int findOrderIsExistByOrderDateAndSetmealIdAndMemberId(@Param("orderDate") String orderDate,
                                                           @Param("setmealId") int setmealId,
                                                           @Param("id") Integer id);

    @Select("select tm.name as member,ts.name as setmeal,tor.ORDERDATE as orderDate,tor.ORDERTYPE as orderType " +
            "from t_member tm,t_setmeal ts,t_order tor " +
            "where tor.SETMEAL_ID=ts.id " +
            "and tor.MEMBER_ID=tm.ID " +
            "and tor.ID=#{orderid}")
    Map findOrderInfoByOrderId(@Param("orderid") Integer orderid);

    @Select("select count(1)  from t_order where ORDERDATE = #{today} and ORDERSTATUS='未到诊'")
    int gettodayOrderNumber(@Param("today") String today);

    @Select("select count(1)  from t_order where ORDERDATE = #{today} and ORDERSTATUS='已到诊'")
    int gettodayVisitsNumber(@Param("today") String today);

    @Select("select count(1)  from t_order where ORDERDATE between #{thisWeekMonday} and #{thisWeekSunday}  and ORDERSTATUS='未到诊'")
    int getthisWeekOrderNumber(@Param("thisWeekMonday") String thisWeekMonday, @Param("thisWeekSunday") String thisWeekSunday);

    @Select("select count(1)  from t_order where ORDERDATE between #{thisWeekMonday} and #{thisWeekSunday}  and ORDERSTATUS='已到诊'")
    int getthisWeekVisitsNumber(@Param("thisWeekMonday") String thisWeekMonday, @Param("thisWeekSunday") String thisWeekSunday);

    @Select("select count(1)  from t_order where ORDERDATE between #{firstDayOfMonth} and #{lastDayOfMonth}  and ORDERSTATUS='未到诊'")
    int getthisMonthOrderNumber(@Param("firstDayOfMonth") String firstDayOfMonth, @Param("lastDayOfMonth") String lastDayOfMonth);

    @Select("select count(1)  from t_order where ORDERDATE between #{firstDayOfMonth} and #{lastDayOfMonth}  and ORDERSTATUS='已到诊'")
    int getthisMonthVisitsNumber(@Param("firstDayOfMonth") String firstDayOfMonth, @Param("lastDayOfMonth") String lastDayOfMonth);


    @Select("SELECT count(tor.SETMEAL_ID) as setmeal_count,ts.name,ts.remark,CONCAT(ROUND(count(tor.SETMEAL_ID)/(select count(1) from t_order)*100,2),'%') as proportion " +
            "FROM  t_order  tor , t_setmeal ts " +
            "where  tor.SETMEAL_ID = ts.ID " +
            "group by tor.SETMEAL_ID " +
            "order by setmeal_count desc " +
            "limit 0,4")
    List<Map> getHotSetmeal();
}
